/*
 * Copyright (C) 2019 ~ 2019 Deepin Technology Co., Ltd.
 *
 * Author:     caitao <caitao@deepin.com>
 *
 * Maintainer: caitao <caitao@deepin.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "brightnesswindow.h"

#include <QDebug>
#include <QVBoxLayout>
#include <QLabel>
#include <QListView>
#include <QLineEdit>

//////控件测试
#include <DToolTip>
#include <DStandardItem>
#include <DListView>
#include <QAbstractItemView>

DWIDGET_USE_NAMESPACE

const QString DisplayInterface("com.deepin.daemon.Display");

BrightnessWindow::BrightnessWindow(QWidget *parent) : QWidget(parent)
{
    m_brightnessSlider = new QSlider(Qt::Horizontal, this);
    m_brightnessSlider->setValue(50);

    QVBoxLayout *contentLayout = new QVBoxLayout(this);
    setLayout(contentLayout);

    contentLayout->addWidget(m_brightnessSlider);

    connect(m_brightnessSlider, &QSlider::valueChanged, this, &BrightnessWindow::adjustBrightness);

    //创建显示对象
    m_displayinter =  new DisplayInter("com.deepin.daemon.Display", "/com/deepin/daemon/Display", QDBusConnection::sessionBus(), this);
    
    QList<QDBusObjectPath> qDbusObjPathLst = m_displayinter->monitors();


    //获取第一个显示屏对象
    m_monitorInter =  new MonitorInter(DisplayInterface, qDbusObjPathLst[0].path(), QDBusConnection::sessionBus(), this);

    //m_displayinter->SetAndSaveBrightness(m_monitorInter->name(), 0.6);

    ////////////控件测试
    ///

    //DToolTip
    DToolTip *toolTip = new DToolTip("this is a dToolTip", false);
    contentLayout->addWidget(toolTip);
    contentLayout->setMargin(100);

    //DViewItemAction、DViewItemActionList
    DViewItemActionList actionLst;
    DViewItemAction *cardAction = new DViewItemAction(Qt::AlignVCenter);
    cardAction->setFontSize(DFontSizeManager::T8);
    cardAction->setTextColorRole(DPalette::TextTips);
    cardAction->setText("接口");

    actionLst.append(cardAction);

    QStandardItemModel *dLstViwMod = new QStandardItemModel;
    DListView *dLstViw = new DListView;
    dLstViw->setSelectionMode(QAbstractItemView::SingleSelection);
    dLstViw->setModel(dLstViwMod);
    dLstViw->setItemMargins(QMargins(0, 0, 0, 0));

    DStandardItem *item = new DStandardItem;
    item->setText("标题");
    item->setFontSize(DFontSizeManager::T5);
    item->setTextColorRole(DPalette::TextTips);
    item->setTextActionList(actionLst);
    dLstViwMod->appendRow(item);

    item = new DStandardItem("属性");
    dLstViwMod->appendRow(item);

    dLstViw->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
    //dLstViw->setLayoutMode(QListView::Batched);
    dLstViw->setStyleSheet("background-color: red");

    contentLayout->addWidget(dLstViw);

    //QLabel
    QLabel *qlabel = new QLabel;
    qlabel->setText("this is a qlabel");
    contentLayout->addWidget(qlabel);


    //margin和padding
    QWidget *marginWidg = new QWidget;
    QHBoxLayout *marginLayout = new QHBoxLayout;
    marginLayout->setMargin(10);
    QWidget *widgetTmp = new QWidget;
    widgetTmp->setStyleSheet("background-color: red");
    marginLayout->addWidget(new QWidget);
    marginWidg->setLayout(marginLayout);
    contentLayout->addWidget(marginWidg);

    //QWidget *paddinWidg = new QWidget;

    //QAbstractItemView
    //QAbstractItemView *abtrIteViw = new QAbstractItemView(this);

    //QListView
    QListView *lstViw = new QListView;
    //lstViw->setFrameShape(QFrame::NoFrame);
    lstViw->setModel(dLstViwMod);
    lstViw->setStyleSheet("background-color: red");

    contentLayout->addWidget(lstViw);

    //QLineEdit
    QLineEdit *lineEdit = new QLineEdit;
    lineEdit->setText("line edite");
    //lineEdit->setBackgroundRole(QPalette::Text);
    //lineEdit->setFrame(true);
    //setBackgroundRole(QPalette::Window);
    //QPalette *paletteTmp = new QPalette();
    lineEdit->setStyleSheet("background-color: rgba(0, 0, 0, 0)");

    contentLayout->addWidget(lineEdit);







}

void BrightnessWindow::adjustBrightness(int inten)
{
//    if (10 > inten) {
//        m_brightnessSlider->setValue(10);
//        inten = 10;
//        return;
//    }

    qDebug()<<m_monitorInter->name() << inten;
    double brightness = inten/100;

    if ( 0.1 > brightness) {
        brightness = 0.1;
    }
    //m_displayinter->SetAndSaveBrightness(m_monitorInter->name(), brightness);
}
